package icloud.com.myapplication.ui.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Service;
import android.content.ContextWrapper;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.support.v4.util.LruCache;
import android.support.v7.app.AppCompatActivity;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

import icloud.com.myapplication.utils.DeviceUuidFactory;
import icloud.com.myapplication.R;
import icloud.com.myapplication.ui.view.TextProgressBar;
import icloud.com.myapplication.ui.view.XWebView;

public class MainActivity extends AppCompatActivity {

    Service mService;
    Activity mActivity;
    ContextWrapper mContextWrapper;

    Fragment mFragment;

    private Button mButtonTest;
    private Button mButtonDelete;
    private EditText mEditText;
    private XWebView mWebView;
    private ProgressBar mPb;
    private TextProgressBar mTextProgressBar;
    private TextView mTV;

    LruCache<String, Bitmap> mLruCache;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //
        mButtonTest = (Button) findViewById(R.id.btn_test);
        mButtonDelete = (Button) findViewById(R.id.btn_delete);
        mEditText = (EditText) findViewById(R.id.et);
        mWebView = (XWebView) findViewById(R.id.webview);
        mPb = (ProgressBar) findViewById(R.id.pb);
        mWebView.setPb(mPb);
        mWebView.init();
        mButtonTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog();
            }
        });
        mButtonDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mEditText.setText("");
            }
        });
        mTextProgressBar = (TextProgressBar) findViewById(R.id.textprogressbar);
        TextView textView = new TextView(this);
        textView.setText("20%");
        textView.setBackgroundColor(getResources().getColor(android.R.color.transparent));
        DeviceUuidFactory factory=new DeviceUuidFactory(this);
        textView.setText(factory.getDeviceUuid().toString());
        ProgressBar pb = new ProgressBar(this);
        pb.setMax(100);
        pb.setProgressDrawableTiled(getResources().getDrawable(R.drawable.progressbar_style, null));
        pb.setProgress(30);
        mTextProgressBar.addView(textView);
        mTextProgressBar.addView(pb);
        mTV = (TextView) findViewById(R.id.tv);
        setFontType(mTV, 2, 4, getResources().getColor(R.color.red));
        findViewById(R.id.btn_jump).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(MainActivity.this, SecondActivity.class), 1000);
            }
        });
        WebView webView2= (WebView) findViewById(R.id.webview2);
        WebSettings webSettings = webView2.getSettings();

        webSettings.setLoadWithOverviewMode(true);
        webSettings.setUseWideViewPort(true);
        webView2.loadUrl("file:///android_asset/a.html");
        findViewById(R.id.btn_test_constanslayout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this,ThirdActivity.class));
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1000 && resultCode == 2000) {
            startActivity(new Intent(MainActivity.this, MyActivity.class));
        }
    }

    /***
     * 显示删除提示dialog
     */
    private void showDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示");
        builder.setMessage("fweghoaeho");
        //监听下方button点击事件
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                //需要判断是从历史记录中删除，还是直接删除服务端数据

            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });
        //设置对话框是可取消的
        builder.setCancelable(true);
        AlertDialog dialog = builder.create();
        dialog.show();
    }

    /***
     * textview前后段显示不同颜色文字
     *
     * @param tv
     * @param start
     * @param end
     * @param color
     */
    public static void setFontType(TextView tv, int start, int end, int
            color) {
        if (tv != null && tv.getText().toString().length() > end) {
            SpannableString span = new SpannableString(tv.getText().toString());
            span.setSpan(new ForegroundColorSpan(color), start, end, Spannable
                    .SPAN_EXCLUSIVE_EXCLUSIVE);
            //第一个参数为字体颜色，第二个和第三个参数为要变色的字体的起始和结尾位置，第四个参数还没搞懂。。。
            tv.setText(span);
        }
    }
}
